package com.example.litf;

import java.util.ArrayList;
import java.util.List;

import com.example.day21.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class WelcomeActivity extends Activity implements OnClickListener{

	private ViewPager viewPager;
	private LinearLayout llPoints;
	private Button btnEnterMain;
	private List<ImageView> ivList;
	private List<View> pointList;
	private MyPagerAdapter adapter;
	private int currentPos = 0; // 记录当前页面的下标

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// 判断是否打开过
		SharedPreferences sp = getSharedPreferences("config", Activity.MODE_PRIVATE);
		boolean hasOpened = sp.getBoolean("hasOpened", false);
		if(hasOpened) {
			// 如果以前打开过，就直接进入主页面
			enterMainActivity();
		} else {
			// 如果以前没有打开过，那就显示当前的导航页面 
			// 记录已经打开过
			Editor editor = sp.edit();
			editor.putBoolean("hasOpened", true);
			editor.commit();
		}
		
//		 requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_welcome);

		getActionBar().hide();
		
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		llPoints = (LinearLayout) findViewById(R.id.ll_point);
		btnEnterMain = (Button) findViewById(R.id.btn_enter_main);

		initData();
		
		adapter = new MyPagerAdapter();
		viewPager.setAdapter(adapter);
		
		registerListener();

	}

	// 设置监听器
	private void registerListener() {
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			// 当页面切换的时候调用的方法，参数position是将要切换到的界面的position
			@Override
			public void onPageSelected(int position) {
				// Log.i("oye", "切换到:"+position);
				pointList.get(currentPos).setEnabled(true);
				pointList.get(position).setEnabled(false);
				currentPos = position;
				if(position == ivList.size()-1) {
					// 如果滑动到了最后一个页面，就显示按钮
					btnEnterMain.setVisibility(View.VISIBLE);
				} else {
					btnEnterMain.setVisibility(View.INVISIBLE);
				}
			}
			
			// 当页面正在滚动的时候调用的方法
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				// Log.i("oye", "onPageScrolled");
			}
			
			/**
			 * 当页面的滚动状态发生改变的时候调用的方法
			 * 		ViewPager.SCROLL_STATE_IDLE，停止
			 * 		ViewPager.SCROLL_STATE_DRAGGING，正在拖拽
			 * 		ViewPager.SCROLL_STATE_SETTLING，固定
			 */
			@Override
			public void onPageScrollStateChanged(int state) {
				switch (state) {
				case ViewPager.SCROLL_STATE_IDLE:
					Log.i("oye", "停止");
					break;
				case ViewPager.SCROLL_STATE_DRAGGING:
					Log.i("oye", "正在拖拽");
					break;
				case ViewPager.SCROLL_STATE_SETTLING:
					Log.i("oye", "固定");
					break;
				default:
					break;
				}
			}
		});
		
		btnEnterMain.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				enterMainActivity();
			}

		});
	}
	
	/**
	 * 进入主界面
	 */
	private void enterMainActivity() {
		Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
	}

	private void initData() {
		// 初始化导航图片
		ivList = new ArrayList<ImageView>();
		int[] resId = { R.drawable.slide1, R.drawable.slide2, R.drawable.slide3 };
		for (int i = 0; i < resId.length; i++) {
			ImageView iv = new ImageView(this);
			LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			iv.setLayoutParams(layoutParams);
			iv.setImageResource(resId[i]);
			iv.setScaleType(ScaleType.CENTER_CROP);
			ivList.add(iv);
		}
		
		// 初始化圆点
		pointList = new ArrayList<View>();
		for (int i = 0; i < resId.length; i++) {
			View point = new View(this);
			point.setBackgroundResource(R.drawable.selector_point);
			LayoutParams params = new LayoutParams(25, 25);
			point.setLayoutParams(params);
			pointList.add(point);
			if(i == 0) {
				// 默认一开始显示的是第一页
				point.setEnabled(false);
			} else {
				point.setEnabled(true);
			}
			// 让每个圆点都记录下自己的下标
			point.setTag(i);
			point.setOnClickListener(this);
			// 将圆点添加到布局中
			llPoints.addView(point);
		}
	}

	/**
	 * ViewPager使用的适配器
	 */
	private class MyPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return ivList != null ? ivList.size() : 0;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(ivList.get(position));
			return ivList.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(ivList.get(position));
		}
	}

	@Override
	public void onClick(View v) {
		// 获取当前被点击的圆点对应的下标
		int pos = (int)v.getTag();
		// 将ViewPager切换到指定页
		viewPager.setCurrentItem(pos);
	}
	
}
